package br.com.appclassificados.helpers;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionFactory {
	
	private String sgdb;
    private String driver;
    private String host;
    private String porta;
    private String banco;
    private String usuario;
    private String senha;
    private Connection conn=null;
    
    private static ConnectionFactory instance;
    
    public Connection getConn(){
    	return this.conn;
    }
    
    public ConnectionFactory(){
        
    	this.sgdb = "jdbc:mysql";
    	this.driver = "com.mysql.jdbc.Driver";
    	this.host = "localhost";
    	this.porta = "3306";
    	this.banco = "classificados";
    	this.usuario = "root";
    	this.senha = "";
    	
    	try{            
             String url;
             url = this.sgdb+"://"+this.host+"/"+this.banco;

             Class.forName(this.driver);            
             conn = DriverManager.getConnection(url, this.usuario, this.senha);
         }catch(ClassNotFoundException eDriver){            
             System.out.println("Driver do banco de dados nao localizado."+eDriver);
         }catch(SQLException e){            
             System.out.println("Erro ao tentar conectar com o banco de dados: "+e);
         }
    }
    
    public static ConnectionFactory getConnection(){
    	//if(instance == null){
    		instance = new ConnectionFactory();
    	//}
    	return instance;
    }
    
    public boolean closeConnection(){
    	try{
    		if(this.conn!=null)
    			this.conn.close();
    		return true;
    	}catch(Exception e){
    		return false;
    	}
    }    
}
